<?php
declare (strict_types = 1);

namespace app\controller\backend\system;

use app\model\communal\ImMessageModel;
use app\service\logic\backend\system\CustomerService;
use app\service\logic\common\order\OrderConsultService;
use app\service\logic\common\user\ExpertService;
use app\validate\common\order\ConsultValidate;
use app\validate\common\system\CustomerValidate;
use app\validate\common\order\MultiValidate;
use app\validate\common\user\ExpertValidate;
use think\App;
use think\Request;
use think\response\Json;
use think\db\exception\DbException;
use app\controller\backend\BackendController;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

/**
 * cp
 */
class CustomerController extends BackendController
{
    protected $service;

    public function __construct(App $app, CustomerService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 客服视图
     *
     * @return string
     */
    public function view()
    {
        return $this->layout('backend/system/customer', 'customer_list');
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);
        $this->validate($params, CustomerValidate::class . '.index');

        $with = ['hasManyExperts', 'byCreator'];
        $fun = function ($model) {
            $genderMap = [0 => '', 1 => '男', 2 => '女'];
            $model['gender_map'] = $genderMap[$model['gender']];
            $model['expert_ids'] = empty($model['hasManyExperts']) ? '' : implode(',', $model['hasManyExperts']->column('id'));
        };
        $result = $this->service->getList($params, $search_fields, $search, $with, $fun);
        return amis($result);
    }

    /**
     * 专家分配
     *
     * @return Json
     */
    public function expert()
    {
        $params = $this->request->param();
        $this->validate($params, CustomerValidate::class . '.expert');

        $result = $this->service->expert($params);
        return amis($result);
    }

    /**
     * 客服端-专家列表
     *
     * @return Json
     */
    public function experts()
    {
        $params = $this->request->param();
        $search_fields = invoke(ExpertService::class)->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, ExpertValidate::class . '.index');
        $customer = $this->request->middleware('admin');
        $search['customer_id'] = $customer['id'];

        $fun = function ($item) {
            $genderMap = [0 => '', 1 => '男', 2 => '女'];
            $item['gender_map'] = $genderMap[$item['gender']];
            unset($item['byCreator']);
            return $item;
        };
        $result = invoke(ExpertService::class)->getList($params, $search_fields, $search, ['byCreator'], $fun);
        return amis($result);
    }

    /**
     * 客服端-专家选项
     *
     * @return Json
     */
    public function exo()
    {
        $params = $this->request->param();
        $search_fields = invoke(ExpertService::class)->getSearchColumn();
        $search = request()->only($search_fields);
        $customer = $this->request->middleware('admin');
        $search['customer_id'] = $customer['id'];

        $drop = invoke(ExpertService::class)->getOptions($params, $search_fields, $search);
        return amis(['options' => $drop]);
    }

    /**
     * 客服端-量表解读|心理资讯-订单列表
     *
     * @return Json
     */
    public function consults(): Json
    {
        $params = $this->request->param();
        $search_fields = invoke(OrderConsultService::class)->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, MultiValidate::class . '.index');

        // 获取该客服所属专家的订单
        $customer = $this->request->middleware('admin');
        $expertIds = $this->service->getExpertIdsBy($customer['id']);
        $search['expert_ids'] = empty($expertIds) ? [-1] : $expertIds;

        $result = invoke(OrderConsultService::class)->getComplexList($params, $search_fields, $search);
        return amis($result);
    }

    /**
     * 客服端-预约时间
     *
     * @return Json
     */
    public function reserve()
    {
        $params = $this->request->param();
        $this->validate($params, MultiValidate::class . '.reserve');

        $result = invoke(OrderConsultService::class)->reserve($params);
        return amis($result);
    }

    /**
     * 发起通话
     *
     * @return Json
     */
    public function call()
    {
        $params = $this->request->param();

        /*
         * 原发起通话
         */
        // $this->validate($params, MultiValidate::class . '.id');
        // $admin = auth('backend')->userOrFail();
        // $params['sender_id'] = $admin['id'];
        // $params['sender_type'] = ImMessageModel::MORPH_TYPE_ONE;
        //
        // $result = invoke(OrderConsultService::class)->call($params);

        /*
         * 客服端-咨询订单变为进行中 abandon
         */
        $params = $this->request->param();
        $this->validate($params, ConsultValidate::class . '.id');

        $result = invoke(OrderConsultService::class)->going($params);
        return amis($result);
    }
}
